/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Area;
import modelo.TipoServicio;

/**
 *
 * @author MANUEL
 */
public class TipoServicioJpaController implements Serializable {

    public TipoServicioJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TipoServicio tipoServicio) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Area numArea = tipoServicio.getNumArea();
            if (numArea != null) {
                numArea = em.getReference(numArea.getClass(), numArea.getNumArea());
                tipoServicio.setNumArea(numArea);
            }
            em.persist(tipoServicio);
            if (numArea != null) {
                numArea.getTipoServicioCollection().add(tipoServicio);
                numArea = em.merge(numArea);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTipoServicio(tipoServicio.getNumTipo()) != null) {
                throw new PreexistingEntityException("TipoServicio " + tipoServicio + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TipoServicio tipoServicio) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoServicio persistentTipoServicio = em.find(TipoServicio.class, tipoServicio.getNumTipo());
            Area numAreaOld = persistentTipoServicio.getNumArea();
            Area numAreaNew = tipoServicio.getNumArea();
            if (numAreaNew != null) {
                numAreaNew = em.getReference(numAreaNew.getClass(), numAreaNew.getNumArea());
                tipoServicio.setNumArea(numAreaNew);
            }
            tipoServicio = em.merge(tipoServicio);
            if (numAreaOld != null && !numAreaOld.equals(numAreaNew)) {
                numAreaOld.getTipoServicioCollection().remove(tipoServicio);
                numAreaOld = em.merge(numAreaOld);
            }
            if (numAreaNew != null && !numAreaNew.equals(numAreaOld)) {
                numAreaNew.getTipoServicioCollection().add(tipoServicio);
                numAreaNew = em.merge(numAreaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tipoServicio.getNumTipo();
                if (findTipoServicio(id) == null) {
                    throw new NonexistentEntityException("The tipoServicio with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoServicio tipoServicio;
            try {
                tipoServicio = em.getReference(TipoServicio.class, id);
                tipoServicio.getNumTipo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tipoServicio with id " + id + " no longer exists.", enfe);
            }
            Area numArea = tipoServicio.getNumArea();
            if (numArea != null) {
                numArea.getTipoServicioCollection().remove(tipoServicio);
                numArea = em.merge(numArea);
            }
            em.remove(tipoServicio);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TipoServicio> findTipoServicioEntities() {
        return findTipoServicioEntities(true, -1, -1);
    }

    public List<TipoServicio> findTipoServicioEntities(int maxResults, int firstResult) {
        return findTipoServicioEntities(false, maxResults, firstResult);
    }

    private List<TipoServicio> findTipoServicioEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(TipoServicio.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TipoServicio findTipoServicio(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TipoServicio.class, id);
        } finally {
            em.close();
        }
    }

    public int getTipoServicioCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<TipoServicio> rt = cq.from(TipoServicio.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
